package com.soft.cake.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.soft.cake.pojo.Address;
import com.soft.cake.pojo.Order;
import com.soft.cake.pojo.OrderProduct;
import com.soft.cake.pojo.Product;
import com.soft.cake.pojo.User;
import com.soft.cake.service.IOrderProductService;
import com.soft.cake.service.IOrderService;
import com.soft.cake.service.IProductService;
import com.soft.cake.service.IUserService;

/**
 * @author Qijuan Liu
 * @description use to controll the page of order
 */
@Controller
public class OrderController {
	
	@Autowired
	private IOrderService orderService;
	@Autowired
	private IUserService userService;
	@Autowired
	private IProductService productService;
	@Autowired
	private IOrderProductService orderProductService;
	@Autowired
	private IOrderProductService opService;
	
	/**
	 * @description 通过userID获取订单列表
	 * @author Qijuan Liu
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("order/{userid}")
	public String listAllOrdersOfUser(@PathVariable("userid") Integer id, Model model) {
		List<Order> orders = orderService.listOrders(id);
		List<OrderProduct> orderProducts = orderProductService.listAllOrderProductsByOrders(orders);
		for(OrderProduct o : orderProducts) {
			o.setProducts(productService.listProductsById(o.getProducts()));
		}
		model.addAttribute("orders", orders);
		model.addAttribute("orderProducts", orderProducts);
		model.addAttribute("orders", orders);
		return "order";
	}
	/**
	 * 
	 * @author CanLig
	 * @date 2019年8月24日 上午10:21:03
	 * @version 1.0.0
	 * @description 显示需要结算的某个订单的信息
	 * @TODO
	 */
	@RequestMapping("/settleorder")
	public String displayOrder(Model model) {
		//Integer userId, Integer orderId正式运行时需要的参数
		Order order = orderService.selOrder(1);
		List<Product> pros = order.getProductsList();
		List<Integer> quantities = new ArrayList<Integer>();
		Map<Product, Integer> products = new HashMap<Product, Integer>();
		for (Product product : pros) {
			product.setImgs(productService.getProductImg(product.getProductId()).getImgs());
			System.out.println("1");
			quantities.add(opService.getProductQuantityInOrder(1, product.getProductId()));
			System.out.println("2");
			products.put(product, opService.getProductQuantityInOrder(1, product.getProductId()));
		}
		System.out.println(products);
		User user = userService.getUserWithAddress(1);
		Address address = user.getUsersAddresses().get(0);
		model.addAttribute("user",user);
		model.addAttribute("order",order);
		model.addAttribute("products", products);
		model.addAttribute("address", address);
		model.addAttribute("quantities", quantities);
		return "settleorder";
	}
	
	/**
	 * @description 通过orderID获取订单详情
	 * @author Qijuan Liu
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("orderinfo/{orderid}")
	public String OrderOfId(@PathVariable("orderid") Integer id,Model model) {
		Order order=orderService.order(id);
		model.addAttribute("orderinnfo",order);
		return "orderinfo";
	}
	
	/**
	 * @description 跳转到订单详情页面
	 * @author Qijuan Liu
	 * @param id
	 * @param model
	 * @return
	 */
	
	
	

}
